package com.pdh.front.filter;

import com.pdh.bean.User;
import com.pdh.util.LoginUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: 彭德华
 * @Date 2021-08-29 22:19
 */
@WebFilter({"/addExpress.html","/expressAssist.html","/expressList.html","/index.html","/lazyBoard.html","/presonQRcode.html","/pickExpress.html","/userCheckStart.html"})
public class UserFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpSession session = ((HttpServletRequest) req).getSession();
        User frontUser = LoginUtil.getFrontUser(session);
        if(frontUser != null) {
            chain.doFilter(req, resp);
        }else {
            ((HttpServletResponse)resp).sendRedirect("login.html");
        }
    }

    @Override
    public void destroy() {

    }
}
